#!/bin/bash

### BEGIN INIT INFO
# Provides:          media-storage
# Required-Start:    mongodb
# Required-Stop:     mongodb
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts media-storage
# Description:       starts the media-storage server
### END INIT INFO

#To run multiple instances, add one config file path and one pidfile path per process, separated by
#spaces or lines; use double-quotes if necessary to encapsulate every definition as a single token
CONFIGFILES=()
PIDFILES=()

#Do not touch these lines, even if using the multi-instance stuff
SCRIPT_NAME=media-storage
DEFAULT_PIDFILE=/var/run/media-storage.pid

start_ms () {
    echo "Starting $SCRIPT_NAME"
    if [ ${#CONFIGFILES[@]} -eq 0 ]; then
        /usr/local/bin/$SCRIPT_NAME &
    else
        for((i=0; i<${#CONFIGFILES[@]}; i++)); do
            echo "Configuring with directives at ${CONFIGFILES[$i]}"
            /usr/local/bin/$SCRIPT_NAME "${CONFIGFILES[$i]}" &
        done
    fi
}

stop_ms () {
    echo "Stopping $SCRIPT_NAME"
    if [ ${#PIDFILES[@]} -eq 0 ]; then
        /bin/kill `cat $PIDFILE`
    else
        for((i=0; i<${#PIDFILES[@]}; i++)); do
            echo "Killing with pidfile at ${PIDFILES[$i]}"
            /bin/kill `cat "${PIDFILES[$i]}"`
        done
    fi
}

case "$1" in
        start)
                start_ms
                ;;
        stop)
                stop_ms
                ;;
        restart)
                echo "Restarting $SCRIPT_NAME"
                stop_ms
                start_ms
                ;;
        *)
                echo "Only the operations 'start', 'stop', and 'restart' are supported"
                exit 1
                ;;
esac
exit 0

